<?php

class User extends CI_Model
{

    var $id = NULL;
    var $name = '';
    var $email = '';
    var $password = '';
    var $tel = '';
    var $permission = 1;

    function __construct()
    {
        parent::__construct();
    }

    function get_last_ten_entries()
    {
        $query = $this->db->get('User', 10);
        return $query->result();
    }

    function get($id)
    {
        $this->db->where("id", $id);
        $this->db->from('user');
        $query = $this->db->get();
        return $query->result();
    }

    public function get_admin()
    {
        $this->db->where("permission", "0");
        $this->db->from('user');
        $query = $this->db->get();
        return $query->result();
    }

    public function get_user()
    {
        $this->db->where("permission", "1");
        $this->db->from('user');
        $query = $this->db->get();
        return $query->result();
    }

    public function get_author()
    {
        $this->db->where("permission", "2");
        $this->db->from('user');
        $query = $this->db->get();
        return $query->result();
    }

    public function count_author()
    {
        $this->db->where("permission", "2");
        $this->db->from('user');
        return $this->db->count_all_results();
    }

    function save()
    {
        $this->db->where('email', $this->input->post('email'));
        $q = $this->db->get('user');
        $data = $q->result_array();

        if (count($data) > 0) return false;

        $this->name = $this->input->post('name');
        $this->email = $this->input->post('email');
        $this->tel = $this->input->post('tel');
        $this->password = md5($this->input->post('password'));
        $this->db->insert('user', $this);

        return true;
    }

    function update($id, $data)
    {
        try {
            $this->db->where("id", $id);
            $this->db->update('user', $data);
        } catch (Exception $e) {
            return 0;
        }
        return 1;
    }

    function login()
    {
        $this->db->select('id, email, password, name, permission');
        $this->db->from('user');
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', MD5($this->input->post('password')));
        $this->db->limit(1);

        $query = $this->db->get();

        if ($query->num_rows() == 1)
            return $query->result();
        return false;
    }

    function edit_permission($id, $permission)
    {
        $this->db->update('user', array('permission' => $permission), "id = '" . $id . "'");
        return true;
    }

    function change_password($email, $password)
    {
        $this->db->where('email', $this->input->post('email'));
        $q = $this->db->get('user');
        $data = $q->result_array();

        if (count($data) == 0) return false;

        $password = md5($password);

        $this->db->query("update user set password = '$password' where email = '$email'");
        return true;
    }

    function get_income($id, $month, $year)
    {
        $query = $this->db->query("select sum(purchasing_price) from purchasing
                                  where purchasing.purchasing_time is not null and month(FROM_UNIXTIME(purchasing_time)) = $month
                                  and user_id=$id and year(FROM_UNIXTIME(purchasing_time)) = $year");
        return $query->result_array()[0];
    }

}